from crawler_core.constant.constants import Constants
from crawler_core.event.task_impl.task_api_down import TaskApiDown
from crawler_core.event.task_impl.task_api_search import TaskApiSearch
from crawler_core.utils.redis_utils import RedisUtils
from lib.sys.logger_factory import LoggerFactory


class TaskAPI(object):
    logger = LoggerFactory.getLogger("TaskAPI")

    def __init__(self):
        pass

    def start(self):
        task_id = RedisUtils.getApiTask()

        if not task_id:
            return
        context = RedisUtils.getEventTask(task_id)
        if not context:
            return
        event_type = context[Constants.CONTEXT_EVENT_TYPE]
        if event_type == Constants.EVENT_TYPE_DOWNLOAD:
            TaskApiDown.start(task_id)
        elif event_type == Constants.EVENT_TYPE_SEARCH:
            TaskApiSearch.start(task_id, context)
